<?php
namespace app\admin\controller;

use think\Loader;
use think\Db;
use app\common\tools\jsonRPCClient;
use think\Lang;

/**
* 用户管理
* @author aierui github  https://github.com/Aierui
* @version 1.0 
*/
class Mall extends Admin
{

	function _initialize()
    {
        parent::_initialize();
    }


    //商城
    public function index()
    {
        $goods = Db::name('goods')->where('status',0)->select();
        $goods = $this->fmtBuylistData($goods);
        $this->assign('goods',$goods);
        return view();
    }


    //格式化数据
    private function fmtBuylistData( $data )
    {
        if(empty($data) && is_array($data)) {
            return $data;
        }

        $lang = Lang::detect();
        

        foreach ($data as $key => $value) {

            $data[$key]['img_src']=$this->getImg($data[$key]['img_src']);
            if($lang=="zh-cn")
            {
                $data[$key]['name']=$data[$key]['name_cn'];
                $data[$key]['remark']=$data[$key]['remark_cn'];
                
            }

            if($lang=="en-us")
            {
                $data[$key]['name']=$data[$key]['name_us'];
                $data[$key]['remark']=$data[$key]['remark_us'];
                
            }

        }

        
        return $data;
    }

    public function getImg($id)
    {
        $img = Db::name('admin_attachment')->where('id',$id)->find();
        return $img['path'];
    }

    //钱包地址
    public function address()
    {

    	$admin = session('admin')['userinfo'];

    	$user = Db::name('user')->where('id',$admin['id'])->find();
    	// print_r($admin);exit;
    	$this->assign('info',$user);
    	return view();
    }

    

    public function pay()
    {
        //查询用户的收件地址
        $admin = session('admin')['userinfo'];

        $user = Db::name('user')->where('id',$admin['id'])->find();
        $id = "";
        if($this->request->isGet())
        {
            $data = $this->request->get();
            
            if(isset($data['id']))
            {
                $id = $data['id'];
            }

        }

        if($this->request->isPost()){
            $data = $this->request->post();
            
            if(isset($data['id']))
            {
                $id = $data['id'];
            }

            

            //更新物流信息
            $address['address'] = $data['address'];
            $address['uid'] = $user['id'];
            $address['consignee'] = $data['consignee'];
            $address['phone'] = $data['phone'];
            $address_id = Db::name('address')->where('uid',$user['id'])->insertGetId($address);

            

            $goods = Db::name('goods')->where('status',0)->where('id',$id)->find();

            $order['total_price'] = $data['price']*$data['num'];
            $order['goods_id'] = $id;
            $order['goods_name_cn'] = $goods['name_cn'];
            $order['goods_name_en'] = $goods['name_us'];
            $order['num'] = $data['num'];
            $order['con_eth'] = $user['eth'];
            $order['bus_eth'] = $goods['eth'];
            $order['address_id'] = $address_id;

            Db::name('order')->insert($order);
            
            if($user<$order['total_price'])
            {                
                $this->error(info(lang('The total amount of the order must not exceed the Transaction value.'),0)['msg']);
            }
            Db::name('user')->where('eth',$order['con_eth'])->setDec('transaction',$order['total_price']);
            Db::name('user')->where('eth',$order['bus_eth'])->setInc('transaction',$order['total_price']*0.8);
            Db::name('user')->where('eth',$order['bus_eth'])->setInc('source',$order['total_price']*0.2);
            $this->success(info(lang('Purchase success'),0)['msg'],'/admin/mall/order');
            
        }

        if($id=="")
        {
            $this->redirect("/admin/mall/index");
        }

        //查询商品
        $goods = Db::name('goods')->where('status',0)->where('id',$id)->find();
        

        if(!$goods)
        {
            $this->redirect("/admin/mall/index");
        }


        

        $address = Db::name('address')->where('uid',$admin['id'])->find();

        $img = Db::name('admin_attachment')->where('id',$goods['img_src'])->find();

        $lang = Lang::detect();
        if($lang = 'zh-cn')
        {
            $goods['name']=  $goods['name_cn'];
            $goods['remark']=  $goods['remark_cn'];
        }
        if($lang = 'en-us')
        {
            $goods['name']=  $goods['name_us'];
            $goods['remark']=  $goods['remark_us'];
        }




        $this->assign('img',$img);
        $this->assign('goods',$goods);
        $this->assign('address',$address);
        return view();

    }

    public function order()
    {
        return view();
    }

    public function getorderlist()
    {
        $admin = session('admin')['userinfo'];

        $user = Db::name('user')->where('id',$admin['id'])->find();


        if(!request()->isAjax()) {
            $this->error(lang('Request type error'), 4001);
        }
        $request = request()->param();
        unset($request['/admin/otc/getorderlist']);
        
        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        
        //$data = Db::name('otc_buy_list')->order('id asc')->where('buy_eth',$info['eth'])->where('status<2')->where( $request['map'] )->select();
        $data = Db::name('order')->where('con_eth',$user['eth'])->order('id desc')->select();
        
        //$data = $this->_fmtData( $data );
        $data = $this->fmtOrderlistData( $data );

        //$data['total'] = 100;
        return $data;

    }

    public function fmtOrderlistData($data)
    {
        if(empty($data) && is_array($data)) {
            return $data;
        }

        $lang = Lang::detect();
        

        foreach ($data as $key => $value) {

            //$data[$key]['img_src']=$this->getImg($data[$key]['img_src']);
            if($lang=="zh-cn")
            {
                $data[$key]['goods_name']=$data[$key]['goods_name_cn'];
                
                
            }

            if($lang=="en-us")
            {
                $data[$key]['goods_name']=$data[$key]['goods_name_en'];
                
                
            }

        }

        
        return $data;
    }


    public function getList()
    {
        if(!request()->isAjax()) {
            $this->error(lang('Request type error'), 4001);
        }
        $request = request()->param();
        unset($request['/admin/otc/getlist']);
        $admin = session('admin')['userinfo'];
        $info = Db::name('ex_address')->where('uid',$admin['id'])->find();
       
        $request = $this->fmtRequest($request);
        //$data = Db::name('otc_sell')->order('id asc')->where('available_num >0')->where( $request['map'] )->limit($request['offset'], $request['limit'])->select();
        $data = Db::name('order')->order('id asc')->where('eth',$info['eth'])->where( $request['map'] )->select();
        //$data = $this->_fmtData( $data );

        return $data;
    }

    public function orderinfo()
    {
        $admin = session('admin')['userinfo'];
        $info = Db::name('ex_address')->where('uid',$admin['id'])->find();
        if($this->request->isGet()){
            $data = $this->request->get();
            $order = Db::name('order')->where('id',$data['id'])->find();
            

            $lang = Lang::detect();

            if($lang=="zh-cn")
            {
                $order['goods_name']=$order['goods_name_cn'];
                
                
            }

            if($lang=="en-us")
            {
                $order['goods_name']=$data['goods_name_en'];
                
                
            }

            


            
            $this->assign('order',$order);
        }
        $this->assign('info',$info);
        return view();
    }

    //外网钱包地址
    public function external()
    {
        $admin = session('admin')['userinfo'];
        $info = Db::name('ex_address')->where('uid',$admin['id'])->find();
        // print_r($admin);exit;
        if($this->request->isPost()){
            $data = $this->request->post();
            unset($data['username']);
            if($info){
                //存在更新
                $res = Db::name('ex_address')->where('uid',$admin['id'])->update($data);
            }else{
                //不存在新增
                $data['uid'] = $admin['id'];
                $res = Db::name('ex_address')->insert($data);
            }

            if($res){
                $this->success('提交成功');
            }else{
                $this->error('提交失败');
            }

        }
        $this->assign('info',$info);
        $this->assign('user',$admin);
        return view();
    }



}